<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>12306后台管理系统 - 登录</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <script src="/admin/js/vue.js"></script>
    <script src="/admin/js/axios.js"></script>
    <script type="text/javascript" src="/home/js/config.js"></script>

    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            background: linear-gradient(135deg, #0d1b2a 0%, #1b263b 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
            color: #e0e1dd;
        }

        .container {
            width: 100%;
            max-width: 450px;
            background: rgba(23, 32, 44, 0.95);
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
            overflow: hidden;
            border: 1px solid rgba(255, 255, 255, 0.1);
        }

        .header {
            background: #1e3a5f;
            color: white;
            padding: 25px;
            text-align: center;
            position: relative;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        }

        .header h1 {
            font-size: 24px;
            font-weight: 600;
            margin-bottom: 5px;
        }

        .header p {
            font-size: 14px;
            opacity: 0.8;
        }

        .login-logo {
            width: 60px;
            height: 60px;
            background: rgba(255, 255, 255, 0.1);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 15px;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
            border: 1px solid rgba(255, 255, 255, 0.15);
        }

        .login-logo i {
            font-size: 30px;
            color: #64b5f6;
        }

        .form-container {
            padding: 30px;
        }

        .form-group {
            margin-bottom: 20px;
            position: relative;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            color: #e0e1dd;
        }

        .input-with-icon {
            position: relative;
        }

        .input-with-icon i {
            position: absolute;
            left: 15px;
            top: 50%;
            transform: translateY(-50%);
            color: #90caf9;
        }

        .form-control {
            width: 100%;
            padding: 12px 15px 12px 45px;
            background: rgba(255, 255, 255, 0.07);
            border: 1px solid rgba(255, 255, 255, 0.15);
            border-radius: 6px;
            font-size: 16px;
            transition: all 0.3s;
            color: #e0e1dd;
        }

        .form-control:focus {
            border-color: #64b5f6;
            outline: none;
            box-shadow: 0 0 0 2px rgba(100, 181, 246, 0.2);
            background: rgba(255, 255, 255, 0.1);
        }

        .form-control::placeholder {
            color: rgba(224, 225, 221, 0.5);
        }

        .btn-login {
            display: block;
            width: 100%;
            padding: 12px;
            background: #1976d2;
            color: white;
            border: none;
            border-radius: 6px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
            margin-top: 10px;
        }

        .btn-login:hover {
            background: #1565c0;
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(25, 118, 210, 0.3);
        }

        .btn-login:active {
            transform: translateY(0);
        }

        .btn-login:disabled {
            background: #455a64;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }

        .error-message {
            color: #f44336;
            font-size: 14px;
            margin-top: 5px;
            height: 20px;
            text-align: center;
        }

        .remember-me {
            display: flex;
            align-items: center;
            margin-top: 10px;
            color: #e0e1dd;
        }

        .remember-me input {
            margin-right: 8px;
            accent-color: #1976d2;
        }

        .login-footer {
            text-align: center;
            padding: 20px;
            color: rgba(224, 225, 221, 0.6);
            font-size: 14px;
            border-top: 1px solid rgba(255, 255, 255, 0.1);
        }

        .login-footer a {
            color: #64b5f6;
            text-decoration: none;
        }

        .login-footer a:hover {
            text-decoration: underline;
        }

        .bg-circle {
            position: absolute;
            border-radius: 50%;
            background: rgba(255, 255, 255, 0.03);
            z-index: -1;
        }

        .circle-1 {
            width: 300px;
            height: 300px;
            top: -100px;
            left: -100px;
        }

        .circle-2 {
            width: 200px;
            height: 200px;
            bottom: -50px;
            right: -50px;
        }

        .circle-3 {
            width: 150px;
            height: 150px;
            top: 30%;
            right: 10%;
        }

        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: translateY(20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .container {
            animation: fadeIn 0.6s ease-out;
        }

        @media (max-width: 480px) {
            .container {
                width: 90%;
            }

            .header {
                padding: 20px;
            }

            .form-container {
                padding: 20px;
            }
        }
    </style>
</head>
<body>
<!-- 背景装饰元素 -->
<div class="bg-circle circle-1"></div>
<div class="bg-circle circle-2"></div>
<div class="bg-circle circle-3"></div>

<div id="app" class="container">
    <div class="header">
        <div class="login-logo">
            <i class="fas fa-train"></i>
        </div>
        <h1>12306后台管理系统</h1>
        <p>铁路运营管理平台</p>
    </div>

    <div class="form-container">
        <form @submit.prevent="login">
            <div class="form-group">
                <label for="username">用户名</label>
                <div class="input-with-icon">
                    <i class="fas fa-user"></i>
                    <input type="text" id="username" class="form-control" placeholder="请输入用户名"
                           v-model="loginForm.username">
                </div>
            </div>

            <div class="form-group">
                <label for="password">密码</label>
                <div class="input-with-icon">
                    <i class="fas fa-lock"></i>
                    <input type="password" id="password" class="form-control" placeholder="请输入密码"
                           v-model="loginForm.password">
                </div>
            </div>

            <div class="remember-me">
                <input type="checkbox" id="remember" v-model="rememberMe">
                <label for="remember">记住我</label>
            </div>

            <div class="error-message">{{ errorMsg }}</div>

            <button type="submit" class="btn-login" :disabled="loading">
                <span v-if="!loading">登录</span>
                <span v-else>
                        <i class="fas fa-spinner fa-spin"></i> 登录中...
                    </span>
            </button>
        </form>
    </div>

    <div class="login-footer">
        <p>© 2025 梦想游- | <a href="#">忘记密码?</a></p>
    </div>
</div>

<script>
    new Vue({
        el: '#app',
        data: {
            loginForm: {
                username: '',
                password: ''
            },
            rememberMe: false,
            errorMsg: '',
            loading: false
        },
        mounted() {
            // 检查是否有保存的登录信息
            const savedUsername = localStorage.getItem('savedUsername');
            if (savedUsername) {
                this.loginForm.username = savedUsername;
                this.rememberMe = true;
            }
        },
        methods: {
            login() {
                // 验证输入
                if (!this.loginForm.username || !this.loginForm.password) {
                    this.errorMsg = '用户名和密码不能为空';
                    return;
                }

                this.loading = true;
                this.errorMsg = '';

                // 修复：使用正确的数据格式发送请求
                axios.post("/home/login", this.loginForm)
                    .then(response => {
                        if (response.data.code === 200) {
                            alert("登陆成功：" + response.data.data.user.username + "欢迎您！！");

                            // 如果选择记住我，保存用户名
                            if (this.rememberMe) {
                                localStorage.setItem('savedAdminName', response.data.data.user.username);
                            } else {
                                localStorage.removeItem('savedAdminName');
                            }

                            // 保存 token 到 localStorage
                            localStorage.setItem('saToken', response.data.data.token);

                            // 新增：保存完整用户信息到 localStorage，供菜单加载器使用
                            const userInfo = {
                                userId: response.data.data.user.userId,
                                username: response.data.data.user.username,
                                roleId: response.data.data.user.roleId || 0, // 确保有roleId字段
                                // 可以根据需要添加其他用户信息字段
                            };
                            localStorage.setItem('userInfo', JSON.stringify(userInfo));

                            // 跳转到首页或之前尝试访问的页面
                            const redirectUrl = sessionStorage.getItem('redirectUrl') || '/admin/index.html';
                            // 清除重定向记录
                            sessionStorage.removeItem('redirectUrl');
                            // 跳转
                            window.location.href = redirectUrl;
                        } else {
                            this.errorMsg = response.data.message || '登录失败，请重试';
                            this.loading = false;
                        }
                    })
                    .catch(error => {
                        console.error('登录请求失败:', error);
                        this.errorMsg = '网络错误，请稍后重试';
                        this.loading = false;
                    });
            }
        }
    });
</script>
</body>
</html>
